<?php
    include_once('Configs/database.php');
     include_once('Models/ThoiKhoaBieu.php');
	 function tietHocTrongTKB($tietbd, $sotiet){
	 	$str = "";
		for ($i=1; $i < $tietbd; $i++){
			$str = $str."&nbsp;-";
		}
		for ($j = $tietbd; $j < ($tietbd + $sotiet); $j++){
			$str = $str."$j";
		}
		for ($m = ($tietbd + $sotiet); $m <= 9; $m++){
			$str = $str."-&nbsp;";
		}
		return $str;
	 }
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        //$hocki = $_POST['cbohocki'];
        //$nienkhoa = $_POST['cbonienkhoa'];
        //$maso = $_POST['cbomaso'];
        $malophp = $_POST['txtmalophp'];
        $siso = $_POST['txtsiso'];
        $thu = $_POST['cbothu'];
        $tietbd = $_POST['cbotietbd'];
        $sotiet = $_POST['cbosotiet'];
        $maphong = $_POST['cbomaphong'];
        $tkb = new ThoiKhoaBieu();
        if (isset($_POST['btnthemtkb'])){
           
            
            $tkb->setMaLopHP($malophp);
            $tkb->setThu($thu);
            $tkb->setTietBD($tietbd);
            $tkb->setSoTiet($sotiet);
            $tkb->setMaPhong($maphong);
			if ($lophp->isExits("ThoiKhoaBieu","malophp='".$malophp."' and thu = ".$thu." and tietbd=".$tietbd."") > 0)
            {
                echo "<script language=javascript> alert('Thời khóa biểu đã tồn tại nên không thể thêm!'); </script>";
               
            }
			else{ 
				if ($tkb->themThoiKhoaBieu() > 0){
					echo "<script language=javascript> alert('Đã thêm thành công!'); </script>";
					
				}
				else{
					echo "<script language=javascript> alert('Không thêm được!'); </script>";
					
				} 
            }
            
        }
        if (isset($_POST['btnxoatkb'])){
            $tkb = new ThoiKhoaBieu();
            $tkb->setMaLopHP($malophp);
            $tkb->setThu($thu);
            $tkb->setTietBD($tietbd);
            if ($tkb->xoaThoiKhoaBieu() > 0){
                echo "<script language=javascript> alert('Đã xóa thành công!'); </script>";
                //require('Views/Capnhatthoikhoabieu.php');
            }
            else{
                echo "<script language=javascript> alert('Không xóa được!'); </script>";
                //require('Views/Capnhatthoikhoabieu.php');
            } 
        }
		 if (isset($_POST['btnsuatkb'])){
            $tkb = new ThoiKhoaBieu();
            $tkb->setMaLopHP($malophp);
            $tkb->setThu($thu);
            $tkb->setTietBD($tietbd);
            $tkb->setSoTiet($sotiet);
            $tkb->setMaPhong($maphong);
            if ($tkb->suaThoiKhoaBieu() > 0){
                echo "<script language=javascript> alert('Đã sửa thành công!'); </script>";
                //require('Views/Capnhatthoikhoabieu.php');
            }
            else{
                echo "<script language=javascript> alert('Không sửa được!'); </script>";
                //require('Views/Capnhatthoikhoabieu.php');
            } 
        }
        if (isset($_POST['btnxem'])){
            $namhoc = $_POST['cbonamhoc'];
            $hocki = $_POST['cbohocki'];
            $maquyen = $_SESSION['nmaquyen'];
            $maso = " ";
            if ($maquyen == 'GV'){
                $maso = $_SESSION['nmaso'];
            }
            else if ($maquyen == 'AD'){
                $maso = $_POST['txtmaso'];
            }
            else{
                $maso = $_POST['cbomaso'];
            }
            $db->setQuery("select concat(holot, ' ', ten) as hoten from CanBo where maso='".$maso."'");
            $result = $db->fetchAll();
            $rows = mysql_fetch_array($result);
            $hoten = $rows['hoten'];
            $query1 = "select distinct thu,a.mahp,kihieu,tenhp,a.malophp,siso,tietbd, sotiet, maso, maphong,a.tuanhoc ";
            $query1 .= "from LopHocPhan a, ThoiKhoaBieu b, HocPhan c ";
            $query1 .= "where maso='".$maso."' and hocki=".$hocki." and namhoc='".$namhoc."' ";
            $query1 .= "and a.malophp = b.malophp and a.mahp = c.mahp ";
            $query1 .= "order by thu,tietbd ";
            $tkb->setQuery($query1);
            $result1 = $tkb->fetchAll();
            $query2 = "select distinct a.malophp,kihieu,a.mahp,tenhp,siso,a.tuanhoc ";
            $query2 .= "from LopHocPhan a, HocPhan b ";
            $query2 .= "where maso='".$maso."' and hocki=".$hocki." and namhoc='".$namhoc."'  and (manhom =5 or manhom =6) ";
            $query2 .= "and a.mahp = b.mahp ";
            $query2 .= "order by a.mahp ";
            $tkb->setQuery($query2);
            //echo $tkb->getQuery();
            $result2 = $tkb->fetchAll();
            require('Views/xemthoikhoabieu.php');
        }
    }
    else{
        if ($_GET['option']=='xemthoikhoabieu'){
            require('Views/xemthoikhoabieu.php');
			//require('Views/Thoikhoabieu.php');
        }
        if ($_GET['option']=='capnhattkb'){
            require('Views/Capnhatthoikhoabieu.php');
        }
        
    }
  
                                
  
?>
